const app = getApp()
var url = require('../../utils/url.js');
var area = require('../../utils/area.js')
var show = false;
var item = {};
var add_id;
var token;
console.log(area)
Page({
  data: {
    item: {
      show: show
    },
    province: "北京市",
    city: "北京市",
    county: "东城区",
    provinceCode: "110000",
    cityCode: "110000",
    countyCode: "110101",
    moren: 0,
    token: "",
    check: false,
    addressInfo: "",
    addressList:true
  },
  onChange(e) {
    console.log(e);
    var that=this;
    that.setData({
      province: e.detail.values[0].name,
      city: e.detail.values[1].name,
      county: e.detail.values[2].name,
      provinceCode: e.detail.values[0].code,
      cityCode: e.detail.values[1].code,
      countyCode: e.detail.values[2].code,
    })
  },
  onChange1(e){
    var that = this;
    that.setData({
      province: e.detail.values[0].name,
      city: e.detail.values[1].name,
      county: e.detail.values[2].name,
      provinceCode: e.detail.values[0].code,
      cityCode: e.detail.values[1].code,
      countyCode: e.detail.values[2].code,
    })
    that.setData({
      addressList:true
    })
  },
  translate:function(){
    this.setData({
      addressList: false
    })
  },
  //生命周期函数--监听页面初次渲染完成
  onReady: function (e) {
    var that = this;
    //请求数据
  },
  onShow() {
    var that = this;
    this.setData({
      areaList: area
    });
  },

  onReachBottom: function () {
  },
  nono: function () { },
  switchChange: function (e) {
    var that = this;
    console.log('switchChange发生change事件，携带value值为：', e.detail.value);
    var b = e.detail.value;
    if (b) {
      that.setData({
        moren: 1
      })
    } else {
      that.setData({
        moren: 0
      })
    }
  },
  onLoad: function (options) {
    // 生命周期函数--监听页面加载
    var that = this;
    console.log(options)
    add_id = options.add_Id;
    if (add_id) {
      var addressInfo = JSON.parse(options.addressInfo)
      var xq1 = addressInfo.region.merger_name.split(",")
      that.setData({
        province: xq1[1],
        city: xq1[2],
        county: xq1[3],
      })
    }
    wx.getStorage({
      key: 'token',
      success: function (res) {
       token = res.data
        that.setData({
          token: token,
        })
        console.log(add_id)
        if (add_id) {
          wx.request({
            url: url.URl + 'my/address/edit',
            data: { id: add_id },
            method: 'get',
            header: {
              'Authorization': token
            },
            success: function (res) {
              console.log(res)
              if (res.data.code == 200) {
                that.setData({
                  addressInfo: res.data.data,
                  provinceCode: res.data.data.province,
                  cityCode: res.data.data.city,
                  countyCode: res.data.data.area,
                  moren: addressInfo.is_default
                })
              }
            },
            fail: function () {
              wx.showToast({
                title: '服务器网络错误!',
                icon: 'loading',
                duration: 1500
              })
            }
          })
        }
      }, fail: function () {
        wx.showModal({
          title: '提示',
          content: '请登录',
          success: function (res) {
            if (res.confirm) {
              wx.switchTab({
                url: '/pages/my/my'
              })

            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
    })
  },
  bindSave: function (e) {
    var that = this;
    var name = e.detail.value.name;
    var address = e.detail.value.address;
    var mobile = e.detail.value.mobile;
    var provinceCode = that.data.provinceCode;
    var cityCode = that.data.cityCode;
    var countyCode = that.data.countyCode;
    var moren = that.data.moren;
    var token = that.data.token
    console.log(provinceCode, cityCode, countyCode)
    if (!add_id) {
      if (name == 0) {
        wx.showToast({
          title: '收货人不能为空',
          icon: 'loading',
          mask: true
        })
      } else if (mobile.length == 0) {
        wx.showToast({
          title: '手机号不能为空',
          icon: 'loading',
          mask: true
        })
      } else if (mobile.length !== 11) {
        wx.showToast({
          title: '手机号长度有误',
          icon: 'loading',
          mask: true
        })
      } else if (cityCode.length == 0 && countyCode.length == 0 && provinceCode.length == 0) {
        wx.showToast({
          title: '请选择地址',
          icon: 'loading',
          mask: true
        })
      } else if (address.length == 0) {
        wx.showToast({
          title: '详细地址不能为空为空',
          icon: 'loading',
          mask: true
        })
      } else {
        wx.request({
          url: url.URl + 'my/address/add',
          data: { province: provinceCode, city: cityCode, area: countyCode, address_detail: address, contact: name, phone: mobile, is_default: moren },
          method: 'POST',
          header: {
            'Authorization': token
          },
          success: function (res) {
            console.log(res)
            var result = res.data;
            var msg = res.data.msg
            if (result.code == 200) {
              wx.showToast({
                title: msg,
                icon: 'success',
                duration: 1000,
                success:function(){
                  setTimeout(() => {
                    // 模拟请求数据，并渲染
                    wx.navigateBack({
                      delta: 1, // 回退前 delta(默认为1) 页面
                      success: function (res) {
                        // success
                        console.log(res)
                      },
                      fail: function () {
                        // fail
                      },
                      complete: function () {
                        // complete
                      }
                    })
                    // 数据成功后，停止下拉刷新
                    wx.stopPullDownRefresh();
                  }, 1000);
                }
              })
              
            }
          }
        })
      }
    } else {
      var id = that.data.add_id
      wx.request({
        url: url.URl + 'my/address/edit',
        data: { province: provinceCode, city: cityCode, area: countyCode, address_detail: address, contact: name, phone: mobile, is_default: moren, id: add_id },
        method: 'POST',
        header: {
          'Authorization': token
        },
        success: function (res) {
          var result = res.data;
          var msg = res.data.msg
          if (result.code == 200) {
            wx.showToast({
              title: msg,
              icon: 'success',
              duration: 1000
            })
            wx.navigateBack({
              delta: 1, // 回退前 delta(默认为1) 页面
              success: function (res) {
                // success
                console.log(res)
              },
              fail: function () {
                // fail
              },
              complete: function () {
                // complete
              }
            })
          }
        }
      })
    }

  },

})